<?php 

function is_blank($object){
	$object = trim($object);
	if ( is_null($object) || (is_string($object) && $object == "") || (is_string($object) && $object == " ") ) return true;
	else return false;
}
function is_number($object){
	if ( !is_blank($object) && ( is_numeric($object) || is_float($object) || is_int($object) ) ) return true;
	else return false;
}

function show_title($title=''){
	if( !$title || !isset($title) || is_blank($title) ) echo DEFAULT_TITLE;
	else echo $title;
}
function get_content_file($content_file_name=''){
	if( !$content_file_name || !isset($content_file_name) || is_blank($content_file_name) ){
		return( VIEW_PATH.DEFAULT_CONTENT_VIEW.EXT);
	}else if(is_file(VIEW_PATH.$content_file_name.EXT)) {
		return( VIEW_PATH.$content_file_name.EXT );
	}else{
		return( VIEW_PATH.DEFAULT_CONTENT_VIEW.EXT);
	}
}

function upload_file($controller,$field_image_name,$upload_path,$new_file_name="",$file_allow_type="",$max_size="",$max_width="",$max_height=""){
	//echo print_r($_FILES[$field_image_name])."<hr>";
	if( !is_blank($controller) && !is_blank($field_image_name) && !is_blank($upload_path) ){
		//Start upload file
		//echo ">>".$field_image_name."<BR>";
		if(!is_blank($new_file_name)){
			$file_type = substr(strrchr($_FILES[$field_image_name]["name"], "."), 0);
			$file_name = $new_file_name.$file_type;
			$_FILES[$field_image_name]["name"]  = $file_name;
		}
		
		if(!is_dir($upload_path)){
			mkdir($upload_path);
		}
		
		$config_upload['upload_path'] = $upload_path;
		if( !is_blank($file_allow_type) ) $config_upload['allowed_types'] = $file_allow_type;
		if( !is_blank($max_size) ) $config_upload['max_size']	= $max_size;
		if( !is_blank($max_width) ) $config_upload['max_width']  = $max_width;
		if( !is_blank($max_height) ) $config_upload['max_height']  = $max_height;
		
		$controller->load->library('upload', $config_upload);
		$controller->upload->initialize($config_upload);
		if ( ! $controller->upload->do_upload($field_image_name))
		{
			return clear_tags($controller->upload->display_errors());
		}	
		else
		{
			//echo print_r($this->upload->data());
			return $controller->upload->data();
		}
	}
}

function upload_image($field_image_name,$upload_path,$new_file_name="",$width=9999,$height=9999,$qulity=99,$ratio=0){
	//echo print_r($_FILES[$field_image_name])."<hr>";
	require_once("./include/image_toolbox.class.php");
	if( !is_blank($field_image_name) && !is_blank($upload_path) ){
		
		if(!is_dir($upload_path)){
			mkdir($upload_path);
		}
		
		$file_info = getimagesize($_FILES[$field_image_name]['tmp_name']);
		$file_width = $file_info[0];
		$file_height['height'] = $file_info[1];
		
		if($file_width < $width) $width = $file_width;
		if($file_height < $height) $height = $file_height;

		$file_type = substr(strrchr($_FILES[$field_image_name]["name"], "."), 1);
		if( $file_type != "jpg" && $file_type != "gif" && $file_type != "png" ){
			$data["error_msg"] = "Only JPG, GIF or PNG be allowed.";
			return $data;
		}
		
		$ITB_thumb = new Image_Toolbox($_FILES[$field_image_name]['tmp_name']);
		$ITB_thumb->newOutputSize($width, $height, $ratio);
		$result = $ITB_thumb->save($upload_path."/".$new_file_name, $file_type, $qulity);

		if($result) {
			$data["file_name"] = $new_file_name;
			$data["error_msg"] = "";
			return $data;
		}else{
			$data["error_msg"] = "error";
			return $result;
		}

	}

}

?>